package user

import (
	"net"
)

type User struct {
	Host   string
	C      chan string
	conn   net.Conn
	Houses map[string]*House
}

func NewUser(conn net.Conn) *User {
	user := &User{
		Host:   conn.RemoteAddr().String(),
		C:      make(chan string),
		conn:   conn,
		Houses: make(map[string]*House),
	}
	go user.listenerMessage()
	return user
}

func (u *User) AddHouse(house *House) {
	u.Houses[house.Name] = house
}

func (u *User) RemoveHouse(houseName string) {
	delete(u.Houses, houseName)
}

func (u *User) MyHouses() map[string]*House {
	return u.Houses
}

func (u *User) listenerMessage() {
	for {
		msg := <-u.C
		u.conn.Write([]byte(msg + "\n"))
	}
}
